<?php

namespace Home\Controller;

use Home\Base\Util;
use Think\Controller;

/**
 * @desc 常用功能
 * @author 张云江
 */
class ApplicationController extends InitController
{
    /**
     * 应用管理页面
     */
    public function appmgr()
    {

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $page_size = 20;

        $total = $MemOrder_Application_info->getCountDataWithAll();
        $pageObj = Util::getPageObject($total, $page_size); //加入分页
        $mempage = $pageObj["mempage"];

        $dataList = $MemOrder_Application_info->getDataWithAllADNPage($mempage);
        $newdataList = array();
        foreach ($dataList as $val) {
            if ($val['is_show_ad'] == 1) {
                $val['ad_show_state'] = '显示';
            } else {
                $val['ad_show_state'] = '隐藏';
            }
            if ($val['application_type'] == 2) {
                $val['application_type_des'] = '安卓';
            } else {
                $val['application_type_des'] = '苹果';
            }
            array_push($newdataList, $val);
        }
        $this->assign("dataList", $newdataList);

        $pageshow = $pageObj["pageshow"];
        $this->assign("page", $pageshow);
        $this->assign("pageindex", $pageObj["mempage"]->getLimitIndex());

        $navPage = array(
            array("name" => "应用列表", "url" => ""),
        );
        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "appmgr");
        $this->display();
    }

    /**
     * 页面
     */
    public function add_application()
    {
        $navPage = array(
            array("name" => "应用列表", "url" => "/application/appmgr.html"),
            array("name" => "添加应用", "url" => ""),
        );

        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "appmgr");
        $this->display();
    }

    /**
     * 添加应用
     */
    public function m_add_application()
    {
        $rtn = array('code' => -1, "notice" => "添加应用失败");

        $ad_app_id = $_POST['ad_app_id'];
        $ad_incentive_id = $_POST['ad_incentive_id'];
        $ad_tail_id = $_POST['ad_tail_id'];
        $ae_materialtype_url = $_POST['ae_materialtype_url'];
        $ae_resource_url = $_POST['ae_resource_url'];
        $application_name = $_POST['application_name'];
        $application_type = $_POST['application_type'];
        $application_password = $_POST['application_password'];
        $environment = $_POST['environment'];
        $build_id = $_POST['build_id'];
        $is_show_ad = $_POST['is_show_ad'];
        $min_version = $_POST['min_version'];
        $version = $_POST['version'];

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();

        $info = $MemOrder_Application_info->getInfoWithbuild_id($build_id);
        if (!empty($info)) {
            $rtn = array('code' => -1, "notice" => "已存在build_id");
            $this->ajaxReturn($rtn);
        }

        $res = $MemOrder_Application_info->insertDataWithParam($application_name, $application_type, $build_id, $version, $min_version, $is_show_ad, $ad_app_id, $ad_tail_id, $ad_incentive_id, $ae_resource_url, $ae_materialtype_url, $application_password, $environment);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "添加应用成功");
        }
        $this->ajaxReturn($rtn);
    }

    /**
     * 页面
     */
    public function edit_application()
    {

        $application_id = $_GET['application_id'];
        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $applicationInfo = $MemOrder_Application_info->getInfoWithid($application_id);

        $this->assign("info", $applicationInfo);

        $navPage = array(
            array("name" => "应用列表", "url" => "/application/appmgr.html"),
            array("name" => "编辑应用", "url" => ""),
        );

        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "appmgr");
        $this->display();
    }
    /**
     * 编辑应用
     */
    public function m_edit_application()
    {
        $rtn = array('code' => -1, "notice" => "修改应用失败");

        $application_id = $_POST['application_id'];
        $ad_app_id = $_POST['ad_app_id'];
        $ad_incentive_id = $_POST['ad_incentive_id'];
        $ad_tail_id = $_POST['ad_tail_id'];
        $ae_materialtype_url = $_POST['ae_materialtype_url'];
        $ae_resource_url = $_POST['ae_resource_url'];
        $application_name = $_POST['application_name'];
        $application_type = $_POST['application_type'];
        $application_password = $_POST['application_password'];
        $environment = $_POST['environment'];
        $build_id = $_POST['build_id'];
        $is_show_ad = $_POST['is_show_ad'];
        $min_version = $_POST['min_version'];
        $version = $_POST['version'];

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $upData = array(
            "ad_app_id" => $ad_app_id,
            "ad_incentive_id" => $ad_incentive_id,
            "ad_tail_id" => $ad_tail_id,
            "ae_materialtype_url" => $ae_materialtype_url,
            "ae_resource_url" => $ae_resource_url,
            "application_name" => $application_name,
            "application_type" => $application_type,
            "application_password" => $application_password,
            "environment" => $environment,
            "build_id" => $build_id,
            "is_show_ad" => $is_show_ad,
            "min_version" => $min_version,
            "version" => $version,
        );
        $wheredata = array(
            'application_id' => $application_id
        );
        $res = $MemOrder_Application_info->updateDataWithParam($upData, $wheredata);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "修改应用成功");
        }
        $this->ajaxReturn($rtn);
    }

    /**
     * 删除应用
     */
    public function m_del_application()
    {
        $rtn = array('code' => -1, "notice" => "删除应用失败");

        $application_id = $_POST['application_id'];

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $upData = array(
            "is_delete" => 1
        );
        $wheredata = array(
            'application_id' => $application_id
        );
        $res = $MemOrder_Application_info->updateDataWithParam($upData, $wheredata);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "删除应用成功");
        }
        $this->ajaxReturn($rtn);
    }


    /**
     * 订阅页面
     */
    public function subscribemgr()
    {

        $MemOrder_Product_info = new \Mem\MemOrder_Product_info();
        $page_size = 20;

        $total = $MemOrder_Product_info->getCountDataWithAll();
        $pageObj = Util::getPageObject($total, $page_size); //加入分页
        $mempage = $pageObj["mempage"];

        $dataList = $MemOrder_Product_info->getDataWithAppliction($mempage);
        $newdataList = array();
        foreach ($dataList as $val) {
            if ($val['state'] == 1) {
                $val['show_state'] = '使用中';
            } else {
                $val['show_state'] = '禁止中';
            }
            array_push($newdataList, $val);
        }
        $this->assign("dataList", $newdataList);

        $pageshow = $pageObj["pageshow"];
        $this->assign("page", $pageshow);
        $this->assign("pageindex", $pageObj["mempage"]->getLimitIndex());

        $navPage = array(
            array("name" => "产品列表", "url" => ""),
        );

        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "subscribemgr");
        $this->display();
    }

    /**
     * 页面
     */
    public function add_subscribe()
    {

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();

        $applicatonList = $MemOrder_Application_info->getDataWithAll();

        $this->assign("applicationList", $applicatonList);

        $navPage = array(
            array("name" => "产品列表", "url" => "/application/subscribemgr.html"),
            array("name" => "添加产品", "url" => ""),
        );

        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "subscribemgr");
        $this->display();
    }

    /**
     * 添加产品
     */
    public function m_add_subscribe()
    {
        $rtn = array('code' => -1, "notice" => "添加产品失败");

        $MemOrder_Product_info = new \Mem\MemOrder_Product_info();

        $application_id = $_POST['application_id'];
        $product_type = $_POST['product_type'];
        $product_channel = $_POST['product_channel'];
        $product_name = $_POST['product_name'];
        $product_time = $_POST['product_time'];
        $pay = $_POST['pay'];
        $state = $_POST['state'];

        $info = $MemOrder_Product_info->getInfoWithproduct_channel($product_channel);
        if (!empty($info)) {
            $rtn = array('code' => -1, "notice" => "已存在渠道号");
            $this->ajaxReturn($rtn);
        }

        $res = $MemOrder_Product_info->insertDataWithParam($application_id, $product_type, $product_channel, $product_name, $product_time, $pay, $state);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "添加产品成功");
        }
        $this->ajaxReturn($rtn);
    }

    /**
     * 页面
     */
    public function edit_subscribe()
    {

        $product_id = $_GET['product_id'];

        $MemOrder_Application_info = new \Mem\MemOrder_Application_info();
        $applicatonList = $MemOrder_Application_info->getDataWithAll();
        $this->assign("applicationList", $applicatonList);

        $MemOrder_Product_info = new \Mem\MemOrder_Product_info();
        $applicationInfo = $MemOrder_Product_info->getInfoWithWithApplictionANDproduct_id($product_id);

        $this->assign("info", $applicationInfo);

        $navPage = array(
            array("name" => "产品列表", "url" => "/application/subscribemgr.html"),
            array("name" => "编辑产品", "url" => ""),
        );

        $this->assign("navPage", $navPage);
        $this->assign("menuFlag", "subscribemgr");
        $this->display();
    }
    /**
     * 编辑产品
     */
    public function m_edit_subscribe()
    {
        $rtn = array('code' => -1, "notice" => "修改产品失败");

        $product_id = $_POST['product_id'];
        $application_id = $_POST['application_id'];
        $product_type = $_POST['product_type'];
        $product_channel = $_POST['product_channel'];
        $product_name = $_POST['product_name'];
        $product_time = $_POST['product_time'];
        $pay = $_POST['pay'];
        $state = $_POST['state'];

        $MemOrder_Product_info = new \Mem\MemOrder_Product_info();
        $upData = array(
            "application_id" => $application_id,
            "product_type" => $product_type,
            "product_channel" => $product_channel,
            "product_name" => $product_name,
            "product_time" => $product_time,
            "pay" => $pay,
            "state" => $state,
        );
        $wheredata = array(
            'product_id' => $product_id
        );
        $res = $MemOrder_Product_info->updateDataWithParam($upData, $wheredata);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "修改产品成功");
        }
        $this->ajaxReturn($rtn);
    }

    /**
     * 删除产品
     */
    public function m_del_subscribe()
    {
        $rtn = array('code' => -1, "notice" => "删除应用失败");

        $product_id = $_POST['product_id'];

        $MemOrder_Product_info = new \Mem\MemOrder_Product_info();
        $upData = array(
            "is_delete" => 1
        );
        $wheredata = array(
            'product_id' => $product_id
        );
        $res = $MemOrder_Product_info->updateDataWithParam($upData, $wheredata);
        if ($res) {
            $rtn = array('code' => 0, "notice" => "删除应用成功");
        }
        $this->ajaxReturn($rtn);
    }
}
